<?php

declare(strict_types=1);

namespace wym\taglib\layui;

use think\Template;
use wym\taglib\Taglib;

class Block extends Taglib
{
    /**
     * 定义标签前缀
     */
    protected string $tag = 'block:';

    /**
     * 定义标签列表
     */
    protected $tags = [
        // 标签定义： attr 属性列表 close 是否闭合（0 或者1 默认1） alias 标签别名 level 嵌套层次
        'input' => ['attr' => '', 'close' => 0, 'alias' => ['text,password,number,textarea,select,switch,radio', 'label']],
    ];

    /**
     * 这是一个Layui样式的闭合标签
     *
     * @param mixed $tag
     */
    public function tagInput(array $tag): string
    {
        $form  = new Form(new Template());
        $label = empty($tag['label']) ? 'div' : $tag['label'];
        $lang  = $tag['lang'] ?? '';
        $parse = '';

        switch ($label) {
            case 'switch':
                unset($tag['mid']);
                $parse = $form->tagCheckbox($tag);
            // no break
            case 'text':
            case 'password':
            case 'number':
                $parse = !empty($parse) ? $parse : $form->tagInput($tag);
            // no break
            case 'textarea':
                $tip   = 'please enter';
                $parse = !empty($parse) ? $parse : $form->tagTextarea($tag, '');
            // no break
            case 'select':
                $tip   = empty($tip) ? 'please select' : $tip;
                $lang  = empty($tag['mid']) ? "'$lang',['$tip']" : $tag['mid'];
                $parse = $form->tagBlock([], (!empty($parse) ? $parse : $form->tagSelect($tag, '')) . (isset($tag['mid']) ? $form->tagMid(['lang' => $lang, 'class' => 'layui-word-aux']) : ''));
                unset($tag['id']);
                $parse = $form->tagItem([], $form->tagLabel($tag) . $parse);
                break;
            case 'radio':
                $parse = $form->tagLabel(['lang' => $lang]);
                $parse .= $form->tagBlock([], $form->tagRadio($tag));
                $parse = $form->tagItem(['id' => ($tag['item'] ?? '')], $parse);
                break;
        }

        return $parse;
    }
}
